package team.seekers.hula.course.structure.chapter.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.hula.course.resource.exam.service.ExamService;
import team.seekers.hula.course.structure.chapter.dao.ChapterDao;
import team.seekers.hula.course.structure.chapter.entity.dao.ChapterDO;
import team.seekers.hula.course.structure.chapter.entity.dto.ChapterQueryListDTO;
import team.seekers.hula.course.structure.section.entity.dao.SectionDO;
import team.seekers.hula.course.structure.section.service.SectionService;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * date: 2022/5/8 20:38
 *
 * @author GH
 * QQ 1124813753
 */
@Service
public class ChapterService {

    @Autowired
    private ChapterDao chapterDao;

    @Autowired
    private SectionService sectionService;

    @Autowired
    private ExamService examService;

    public List<ChapterQueryListDTO> getSimpleCourseDirsByCourseId(String courseId) {

        List<ChapterDO> list = chapterDao.lambdaQuery().eq(ChapterDO::getCourseId, courseId).list();
        return list.stream().map(chapterDO ->
                ChapterQueryListDTO.builder()
                        .id(chapterDO.getId())
                        .name(chapterDO.getName())
//                        .exams(examService.querySimpleCourseExamsByChapterId(chapterDO.getId()))
                        .sections(sectionService.querySimpleSectionsByChapterId(chapterDO.getId()))
                        .build()).collect(Collectors.toList());
    }

    public List<ChapterQueryListDTO> getDetailCourseDirsByCourseId(String courseId, String userId) {
        List<ChapterDO> list = chapterDao.lambdaQuery().eq(ChapterDO::getCourseId, courseId).list();

        return list.stream().map(chapterDO ->
                ChapterQueryListDTO.builder()
                        .id(chapterDO.getId())
                        .name(chapterDO.getName())
//                        .exams(examService.queryDetailCourseExamsByChapterId(chapterDO.getId()))
                        .sections(sectionService.queryDetailSectionsByChapterId(chapterDO.getId(), userId))
                        .build()).collect(Collectors.toList());
    }


    public List<String> queryChapterIdsByCourseId(String courseId) {
        List<ChapterDO> list = chapterDao.lambdaQuery().select(
                ChapterDO::getId,
                ChapterDO::getCourseId
        ).eq(ChapterDO::getCourseId, courseId).list();

        List<String> lists = new ArrayList<>();

        list.forEach(chapterDO -> {
            lists.add(chapterDO.getId());
        });

        return lists;
    }

    public Integer countByCourseId(String id) {
        return chapterDao.lambdaQuery().eq(ChapterDO::getCourseId,id).count();
    }
}
